<template>
    <div class="login_container">
       <div class="login_box">
           <!-- 头像区域 -->
         <div class="avatar_box">
             <img src="../../assets/img/logo.png" alt="">
         </div>
          <!-- 登录区域 -->
            <el-form class="login_form" :model="loginForm" :rules="loginFromRules" ref="loginFormRef">
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" prefix-icon="el-icon-lock" type="password"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="resetting">重置</el-button>
                </el-form-item>                    
           </el-form>
       </div>
    </div>
</template>

<script>
// import {request} from '../../network/request'
export default {
    data () {
        return{
            loginForm:{
                username: 'admin',
                password: '123456'
            },
            // 表单验证
            loginFromRules:{
              username:[
                  { required: true, message: '请输入用户名称', trigger: 'blur' },
                  { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
              ],
              password:[
                  { required: true, message: '请输入密码', trigger: 'blur' },
                  { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
              ]
            }
        }
    },
    created() {
 
    },
    methods:{
        // 重置
        resetting () {
         this.$refs.loginFormRef.resetFields()
        },
        login () {
      this.$refs.loginFormRef.validate(async valid => {
        // 如果返回失败就停止执行
          if (!valid){
             return  this.$message.error('登录失败');
          } else{
            const res = await this.$http.post('login', this.loginForm)
            console.log(res.data)
            this.$message.success('登录成功');
            // 登录成功后保存token
            window.sessionStorage.setItem('token',res.data.data.token)
            //跳转
            this.$router.push('/home')
          }
      })
        }
    }
}
</script>

<style lang="less" scoped>
   .login_container{
       background:#2b4b6b;
       height: 100%;
       display: flex;
       justify-content:center;
      align-items: center;
       .login_box{
           width: 450px;
           height: 300px;
           background: #ffffff;
           border-radius: 5px;
           position: relative;
          .avatar_box{
              width: 130px;
              height: 130px;
              border: 1px solid #eee;
              border-radius: 50%;
              padding: 10px;
              position: absolute;
              left: 50%;
              margin-left: -75px;
              margin-top: -75px;
              box-shadow: 0px 0px 10px #ddd;
              background: #ffffff;
              img{
                 width: 100%;
                 height: 100%;
                 border-radius: 50%;
                 background: #eee;
              }
          }
          .login_form{
              position: absolute;
              bottom: 0;
              width: 100%;
              padding: 20px;
              box-sizing: border-box;
            .btns{
              display: flex;
              justify-content: flex-end;
          }
          }

       }
   }
</style>